﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Runtime.Remoting.Messaging;
using System.IO;

namespace MyDBconnection
{

    public struct UpdateQty
    {
        public string qty;
    }
    public struct settings
    {
        public string host, db, uid, pass;
    }
    public class DatabaseConnection
    {
        OdbcConnection oc = new OdbcConnection();
        OdbcDataAdapter oa = new OdbcDataAdapter();
        public DataTable tbl = new DataTable();
        settings server = new settings();
        
        
        public void dbconnection()
        {
            string[] lines = File.ReadAllLines(@".\\Connection_Settings.txt");
            server.host = lines[0];
            server.db = lines[1];
            server.uid = lines[2];
            server.pass = lines[3];

            oc.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};" +
                    "Server=" + server.host+";"+
                    "Database=" + server.db +";"+
                    "UID=Jlerry;" +server.uid+";"+
                    "PWD="+server.pass+";";
            oc.Open();
                    
              
        }
     
    

        public void DBClose()
        {
            oc.Close();
        }


        #region login
        //async sa login
        delegate void myloginThread(string login);
        public void loginOperation(string login)
        {
            
            myloginThread mlt = new myloginThread(mylogin);
            AsyncCallback ac = new AsyncCallback(CallBackMethod);
            IAsyncResult ar = mlt.BeginInvoke(login, ac, null);
        }
        
        void CallBackMethod(IAsyncResult ar)
        {

            myloginThread mlt = (myloginThread)((AsyncResult)ar).AsyncDelegate;
            mlt.EndInvoke(ar);
        }

        public void mylogin(string login)
        {
            tbl = new DataTable();
            oa.SelectCommand = new OdbcCommand(login, oc);
            tbl.Clear();
            tbl.Reset();
            oa.Fill(tbl);
       
        }
        #endregion

        #region loaddata
        delegate void loaddataThread(string load);
        public void loaddataOperation(string load)
        {

            myloginThread ldt = new myloginThread(loadData);
            AsyncCallback ac = new AsyncCallback(LDCallBackMethod);
            IAsyncResult ar = ldt.BeginInvoke(load, ac, null);
        }
        public int AsyncComplete = 0;
        void LDCallBackMethod(IAsyncResult ar)
        {

            loaddataThread ldt = (loaddataThread)((AsyncResult)ar).AsyncDelegate;
            ldt.EndInvoke(ar);   
        }

        public void loadData(string load)
        {
            tbl = new DataTable();
            tbl.Clear();
            tbl.Reset();
            oa.SelectCommand = new OdbcCommand(load, oc);
            oa.Fill(tbl);
        }
        #endregion

        #region addData
        delegate void addDataThread(string add);
        public void addDataOperation(string add)
        {

            addDataThread adt = new addDataThread(AddData);
            AsyncCallback ac = new AsyncCallback(addDataCallBackMethod);
            IAsyncResult ar = adt.BeginInvoke(add, ac, null);
        }
        
        void addDataCallBackMethod(IAsyncResult ar)
        {

            addDataThread adt = (addDataThread)((AsyncResult)ar).AsyncDelegate;
            adt.EndInvoke(ar);   
        }
        public void AddData(string add)
        {
            tbl = new DataTable();
            tbl.Clear();
            tbl.Reset();
            oa.SelectCommand = new OdbcCommand(add, oc);
            oa.Fill(tbl);

        }
        #endregion

        #region updatepassword
        delegate void updatePassThread(string update);
        public void updatePassOperation(string update)
        {

            updatePassThread upt = new updatePassThread(UpdatePassword);
            AsyncCallback ac = new AsyncCallback(updatePassCallBackMethod);
            IAsyncResult ar = upt.BeginInvoke(update, ac, null);
        }
        
        void updatePassCallBackMethod(IAsyncResult ar)
        {

            updatePassThread upt = (updatePassThread)((AsyncResult)ar).AsyncDelegate;
            upt.EndInvoke(ar);
            
        }

        public void UpdatePassword(string update)
        {
            try
            {
                oa.SelectCommand = new OdbcCommand(update, oc);
                oa.Fill(tbl);
             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            
            }
        }
        #endregion

        #region loadproduct
        delegate void loadproductThread(string product);
        public void loadproductOperation(string product)
        {

            loadproductThread lpt = new loadproductThread(loadproduct);
            AsyncCallback ac = new AsyncCallback(loadproductCallBackMethod);
            IAsyncResult ar = lpt.BeginInvoke(product, ac, null);
        }
        
        void loadproductCallBackMethod(IAsyncResult ar)
        {

            loadproductThread lpt = (loadproductThread)((AsyncResult)ar).AsyncDelegate;
            lpt.EndInvoke(ar);
            
        }

        public void loadproduct(string product)
        {
            tbl = new DataTable();
            oa.SelectCommand = new OdbcCommand(product, oc);
            oa.Fill(tbl);
    
        }
        #endregion
        public void produpdate(string update)
        {
            tbl = new DataTable();
            oa.SelectCommand = new OdbcCommand(update, oc);
            oa.Fill(tbl);

        }

        public void UpdateQuantity(UpdateQty Qty, int i)
        {
            tbl.Rows[i][2] = Qty.qty;

            OdbcCommandBuilder ocb = new OdbcCommandBuilder(oa);
            oa.Update(tbl);
        }
    }
}